//
//  HomeViewController.m
//  EarnCommision
//
//  Created by EvanZ on 15/3/12.
//  Copyright (c) 2015年 EC. All rights reserved.
//

#import "HomeViewController.h"
#import "IIViewDeckController.h"
#import "UIViewExt.h"
#import "ActivityTableViewController.h"
#import "PlatformOperateTableViewController.h"
#import "MyCommissionViewController.h"
#import "MyOrderViewController.h"

#define kScreenHeight [UIScreen mainScreen].bounds.size.height
#define kScreenWidth [UIScreen mainScreen].bounds.size.width

@interface HomeViewController ()
@property (strong,nonatomic) UIScrollView *scrollView;
@property (strong,nonatomic) UIPageControl *pageControl;

@property (strong,nonatomic) UIButton *myCommissionBtn;
@property (strong,nonatomic) UIButton *nearbyBtn;
@property (strong,nonatomic) UIButton *orderBtn;
@property (strong,nonatomic) UIButton *serviceBtn;

@property (strong,nonatomic) UIButton *ad1_pt;
@property (strong,nonatomic) UIButton *ad2_jc;
@property (strong,nonatomic) UIButton *ad3_wl;
@property (strong,nonatomic) UIButton *ad4_nz;
@property (strong,nonatomic) UIButton *ad5_fdc;
@property (strong,nonatomic) UIButton *ad6_qmz;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"赚佣金";
    self.view.backgroundColor = [UIColor whiteColor];
    
    UIBarButtonItem * leftButton = [[UIBarButtonItem alloc]initWithTitle:@"open" style:UIBarButtonItemStylePlain target:self action:@selector(openLeftDeck)];
    self.navigationItem.leftBarButtonItem = leftButton;
    
    float scale = kScreenWidth/320.0;
    _scrollView  = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, scale*120)];
    [_scrollView setContentSize:CGSizeMake(kScreenWidth * 4, 0)];
    
    [_scrollView setPagingEnabled:YES];
    [_scrollView setShowsHorizontalScrollIndicator:NO];
    [_scrollView setShowsVerticalScrollIndicator:NO];

    
    [_scrollView setDelegate:self];
    
    for (int i =0; i<4; i++)
    {
        
        UIButton *imgBtn = [[UIButton alloc]initWithFrame:CGRectMake(i * kScreenWidth,0,kScreenWidth,scale*120)];
        [imgBtn setBackgroundImage:[UIImage imageNamed:[NSString stringWithFormat:@"home_barner_%d",i+1]] forState:UIControlStateNormal];
        imgBtn.tag = 1000+i;
        [imgBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_scrollView addSubview:imgBtn];
        
       
    }
    [self.view addSubview:_scrollView];
    
    
    _pageControl = [[UIPageControl alloc]initWithFrame:CGRectMake(kScreenWidth-80, _scrollView.bottom-20, 80, 10)];
    [_pageControl setNumberOfPages:4];
    //监听页面是否发生改变
    [_pageControl addTarget:self action:@selector(changePage) forControlEvents:UIControlEventValueChanged];
    [self.view addSubview:_pageControl];
    
    
    _myCommissionBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, _scrollView.bottom, kScreenWidth/4, scale*107)];
    [_myCommissionBtn setImage:[UIImage imageNamed:@"home_page_my_commission.png"] forState:UIControlStateNormal];
    [_myCommissionBtn setShowsTouchWhenHighlighted:YES];
    [_myCommissionBtn addTarget:self action:@selector(myCommissionBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _serviceBtn = [[UIButton alloc]initWithFrame:CGRectMake(_myCommissionBtn.right, _scrollView.bottom, kScreenWidth/4, scale*107)];
    [_serviceBtn setImage:[UIImage imageNamed:@"home_page_service.png"] forState:UIControlStateNormal];
    [_serviceBtn setShowsTouchWhenHighlighted:YES];
    [_serviceBtn addTarget:self action:@selector(serviceBtnClick) forControlEvents:UIControlEventTouchUpInside];
    
    _orderBtn = [[UIButton alloc]initWithFrame:CGRectMake(_serviceBtn.right, _scrollView.bottom, kScreenWidth/4, scale*107)];
    [_orderBtn setImage:[UIImage imageNamed:@"home_page_order.png"] forState:UIControlStateNormal];
    [_orderBtn addTarget:self action:@selector(orderBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [_orderBtn setShowsTouchWhenHighlighted:YES];
    
    _nearbyBtn = [[UIButton alloc]initWithFrame:CGRectMake(_orderBtn.right, _scrollView.bottom, kScreenWidth/4, scale*107)];
    [_nearbyBtn setImage:[UIImage imageNamed:@"home_page_nearby.png"] forState:UIControlStateNormal];
    [_nearbyBtn setShowsTouchWhenHighlighted:YES];
    
    float ad_w = scale*164;
    _ad1_pt= [[UIButton alloc]initWithFrame:CGRectMake(0, _serviceBtn.bottom, kScreenWidth/3.0, ad_w)];
    //[_ad1_pt setBackgroundColor:[UIColor blackColor]];
    [_ad1_pt setImage:[UIImage imageNamed:@"image_block_1.png"] forState:UIControlStateNormal];
   // [_ad1_pt setContentMode:UIViewContentModeScaleAspectFit];
    
    
    _ad2_jc= [[UIButton alloc]initWithFrame:CGRectMake(_ad1_pt.right, _serviceBtn.bottom, kScreenWidth/3.0, ad_w)];
    [_ad2_jc setImage:[UIImage imageNamed:@"image_block_2.png"] forState:UIControlStateNormal];
    
    _ad3_wl= [[UIButton alloc]initWithFrame:CGRectMake(_ad2_jc.right, _serviceBtn.bottom, kScreenWidth/3.0, ad_w)];
    [_ad3_wl setImage:[UIImage imageNamed:@"image_block_3.png"] forState:UIControlStateNormal];
    
    _ad4_nz= [[UIButton alloc]initWithFrame:CGRectMake(0, _ad1_pt.bottom, kScreenWidth/3.0, ad_w)];
    [_ad4_nz setImage:[UIImage imageNamed:@"image_block_4.png"] forState:UIControlStateNormal];
    //[_ad4_nz setBackgroundColor:[UIColor blackColor]];
    
    _ad5_fdc= [[UIButton alloc]initWithFrame:CGRectMake(_ad4_nz.right, _ad1_pt.bottom, kScreenWidth/3.0, ad_w)];
    [_ad5_fdc setImage:[UIImage imageNamed:@"image_block_5.png"] forState:UIControlStateNormal];
    
    _ad6_qmz= [[UIButton alloc]initWithFrame:CGRectMake(_ad5_fdc.right, _ad1_pt.bottom, kScreenWidth/3.0, ad_w)];
    [_ad6_qmz setImage:[UIImage imageNamed:@"image_block_6.png"] forState:UIControlStateNormal];
    
    [self.view addSubview:_myCommissionBtn];
    [self.view addSubview:_serviceBtn];
    [self.view addSubview:_orderBtn];
    [self.view addSubview:_nearbyBtn];
    [self.view addSubview:_ad1_pt];
    [self.view addSubview:_ad2_jc];
    [self.view addSubview:_ad3_wl];
    [self.view addSubview:_ad4_nz];
    [self.view addSubview:_ad5_fdc];
    [self.view addSubview:_ad6_qmz];
    
}


-(void)btnClick:(UIButton*)sender
{
    int tag = sender.tag;
    if (tag == 1000)
    {
        PlatformOperateTableViewController *platformOperateController = [[PlatformOperateTableViewController alloc]init];
        [self.navigationController pushViewController:platformOperateController animated:YES];
    }
    else if(tag == 1001)
    {
        ActivityTableViewController *actController = [[ActivityTableViewController alloc]init];
        [self.navigationController pushViewController:actController animated:YES];
    }
}
-(void)myCommissionBtnClick
{
    MyCommissionViewController *myComViewController = [[MyCommissionViewController alloc]init];
    [self.navigationController pushViewController:myComViewController animated:YES];
}
-(void)orderBtnClick
{
    MyOrderViewController *myOrderViewController = [[MyOrderViewController alloc]init];
     [self.navigationController pushViewController:myOrderViewController animated:YES];
}
-(void)serviceBtnClick
{
    UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"客服电话" message:@"18811442526" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:@"呼叫"];
    
    [alter show];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)openLeftDeck{
    [self.viewDeckController toggleLeftViewAnimated:YES];
}

-(void)scrollViewDidScroll:(UIScrollView*)scrollView

{
    [_pageControl setCurrentPage:fabs(scrollView.contentOffset.x/self.view.frame.size.width)];
}


@end
